package com.yyt.jasyptdemo.utils;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author yeyangtao created at 11:55 2020/8/10
 */
@Component
public class EncryptUtil {
    @Resource
    private StringEncryptor stringEncryptor;

    /**
     * 加密，采用配置文件中的盐
     *
     * @param str
     * @return
     */
    public String encrypt(String str) {
        return stringEncryptor.encrypt(str);
    }

    /**
     * 解密，采用配置文件中的盐
     *
     * @param str
     * @return
     */
    public String decrypt(String str) {
        return stringEncryptor.decrypt(str);
    }

    /**
     * 加密
     *
     * @param str
     * @param slat
     * @return
     */
    public String encrypt(String str, String slat) {
        BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
        basicTextEncryptor.setPassword(slat);
        return basicTextEncryptor.encrypt(str);
    }

    /**
     * 解密
     *
     * @param str
     * @return
     */
    public String decrypt(String str, String slat) {
        BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
        basicTextEncryptor.setPassword(slat);
        return basicTextEncryptor.decrypt(str);
    }
}
